<?php
/**
 * Firal CMS
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to firal-dev@googlegroups.com so we can send you a copy immediately.
 *
 * @category   Firal
 * @package    Firal_Cms
 * @copyright  Copyright (c) 2008 MaakSite.nl Productions. (http://productions.maaksite.nl/)
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 * @version    $Id: global.php 11 2008-06-30 16:14:44Z pieterkokx $
 */

// should be changed later (development or production stage)
error_reporting(E_ALL | E_STRICT);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);

// should be in configuration
date_default_timezone_set('Europe/Amsterdam');

/**
 * The root directory
 */
define('ROOT', realpath(dirname(__FILE__) . '/../'));


/**
 * Add some directories to the include path
 * 
 * @param string|array $path
 * 
 * @return void
 */
function addToIncludePath($path)
{
    if (is_string($path)) {
        $path = (array) $path;
    } elseif (!is_array($path)) {
        // TODO: throw error
    }
    
    $path[] = get_include_path();
    
    set_include_path(implode(PATH_SEPARATOR, $path));
}

addToIncludePath(array(
    ROOT . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'zend',
    ROOT . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'doctrine',
    ROOT . DIRECTORY_SEPARATOR . 'app'     . DIRECTORY_SEPARATOR . 'models',
    ROOT . DIRECTORY_SEPARATOR . 'app'     . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'generated'
));

/**
 * @see Zend_Loader
 */
require 'Zend/Loader.php';

Zend_Loader::registerAutoload('Doctrine'); 

$doctrineConfig = array(
    'data_fixtures_path'  =>  ROOT . '/data/doctrine/data/fixtures',
    'models_path'         =>  ROOT . '/app/models',
    'migrations_path'     =>  ROOT . '/data/doctrine/migrations',
    'sql_path'            =>  ROOT . '/data/doctrine/data/sql',
    'yaml_schema_path'    =>  ROOT . '/data/doctrine/schema'
);

/**
 * @see Zend_Registry
 */
require_once 'Zend/Registry.php';

Zend_Registry::set('doctrine_config', $doctrineConfig);

/**
 * @see Zend_Config_Ini
 */
require_once 'Zend/Config/Ini.php';

$config = new Zend_Config_Ini(ROOT . '/data/config/config.ini', 'development');

Zend_Registry::set('config', $config);

/**
 * @see Zend_Debug
 */
require_once 'Zend/Debug.php';

if (extension_loaded('xdebug')) {
    Zend_Debug::setSapi('cli');
}

$connectionManager = Doctrine_Manager::getInstance();


$connectionManager->openConnection($config->db->dsn, 'Firal_Default');

//$connectionManager->setAttribute(Doctrine::ATTR_NAME_PREFIX, $config->db->table_prefix);

// configure the cache

$backendAdapter = strtolower($config->cache->backendAdapter);

$connectionString = '';

if (($backendAdapter != 'file') && ($backendAdapter != 'sqlite')) {
    $connectionString = 'Firal_Cache_Tags';
    
    $conn = $connectionManager->openConnection($config->cache->tags->db->dsn, $connectionString);
    
    $conn->createDatabase('sqlite');
    
    $connectionManager->setCurrentConnection('Firal_Default');
}

/**
 * @see Zend_Cache_Core
 */
require_once 'Zend/Cache/Core.php';
/**
 * @see Firal_Cache_Backend_Firal
 */
require_once 'Firal/Cache/Backend/Firal.php';

$backend = new Firal_Cache_Backend_Firal($config->cache->backendAdapter, $config->cache->backend->toArray(), $connectionString);

$frontend = new Zend_Cache_Core(array(
    'automatic_serialization' => true,
    'lifetime'                => 3600,
    'caching'                 => (bool) $config->cache->caching
));

$frontend->setBackend($backend);

Zend_Registry::set('firal_cache', $frontend);
Zend_Registry::set('firal_cache_backend', $backend);
Zend_Registry::set('firal_cache_options', array('caching' => $config->cache->caching));